package com.yidi.system.service.imp;

import com.yidi.common.core.service.imp.AbstractBaseService;
import com.yidi.common.core.text.Convert;
import com.yidi.common.enums.RemoveEnum;
import com.yidi.common.utils.StringUtils;

import com.yidi.system.domain.SysOperLog;
import com.yidi.system.repository.SysOperLogRepository;
import com.yidi.system.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：Xn
 * @ClassName: SysOperLogServiceImpl
 * @date ：2021-10-19 14:17
 * @describe ：
 */
@Service
public class SysOperLogServiceImpl extends AbstractBaseService<SysOperLog> implements SysOperLogService {

    @Autowired
    private SysOperLogRepository sysOperLogRepository;

    /**
     * 分页
     */
    @Override
    public Page<SysOperLog> findByPage(Integer pageNo, Integer pageSize, SysOperLog operLog) {
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC, "operTime"));
        Sort sort = Sort.by(orders);
        Pageable pageable = PageRequest.of(pageNo, pageSize, sort);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Page<SysOperLog> page = sysOperLogRepository.findAll((root, query, cb) ->{
            List<Predicate> list = new ArrayList<>();
            list.add(cb.and(cb.equal(root.get("removeStatus").as(Integer.class), RemoveEnum.NOT_REMOVE.getCode())));

            if(StringUtils.isNotBlank(operLog.getTitle())) {
                list.add(cb.and(cb.like(root.get("title").as(String.class), operLog.getTitle())));
            }

            if(operLog.getBusinessType() != null) {
                list.add(cb.and(cb.equal(root.get("businessType").as(Integer.class), operLog.getBusinessType())));
            }

            if(operLog.getStatus() != null) {
                list.add(cb.and(cb.equal(root.get("status").as(Integer.class), operLog.getStatus())));
            }

            if(StringUtils.isNotBlank(operLog.getOperName())) {
                list.add(cb.and(cb.like(root.get("operName").as(String.class), operLog.getOperName())));
            }

            Map<String, Object> params = operLog.getParams();
            if(params != null) {
                String beginTime = (String) params.get("beginTime");
                String endTime = (String) params.get("endTime");
                if(StringUtils.isNotBlank(beginTime)) {
                    try {
                        list.add(cb.and(cb.greaterThanOrEqualTo(root.get("operTime"), sdf.parse(beginTime))));
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(StringUtils.isNotBlank(endTime)) {
                    try {
                        list.add(cb.and(cb.lessThanOrEqualTo(root.get("operTime"), sdf.parse(endTime))));
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        },pageable);

        return page;
    }

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog)
    {
        sysOperLogRepository.save(operLog);
    }

    /**
     * 批量删除操作日志
     *
     * @param ids 需要删除的数据
     * @return
     */
    @Override
    public void deleteOperLogByIds(Long[] ids) {

        for(Long id : ids) {
            deleteOperLogById(id);
        }
    }

    /**
     * 删除操作日志
     * @param operLogId 操作日志ID
     */
    @Override
    public void deleteOperLogById(Long operLogId) {

        SysOperLog operLog = findById(operLogId);
        if(operLog != null) {
            delete(operLogId);
        }
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        deleteAll();
    }
}
